const process = require("process");
const fs = require('fs');
const childProcess = require('child_process');
const shellPath = `${process.cwd()}/shell/`;

const identity = (hook={}) => {
    const { password } = typeof hook===`string` ? JSON.parse(hook) : hook ;
    return password === `UQJtOrSO7RF0wokbZyB6WOJSJAY9yxLDUqiS7xNm`;
};

const homeRender = async () => {
    const files = fs.readdirSync(shellPath);
    return await files.map(name=>{
        return name.replace('.sh','');
    });
};

const cnodeSync = async hook => {
    if(!identity(hook)) {
        return await Promise.reject(`认证失败`);
    }
    return new Promise((resolve, reject)=>{
        childProcess.exec(`${shellPath}cnode.sh`,( err, stdout, stderr ) => {
            console.log(`--------stdout--------`);
            if(err) {
                reject('失败');
            } else {
                resolve('成功');
            }
        });
    });
};

exports.cnodeSync = cnodeSync;

exports.homeRender = homeRender;